﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RadowCore.Models
{
    /// <summary>
    /// A node in the exception tree which represents a DNF among ConjunctNode objects.
    /// </summary>
    class DisjunctNode:TreeNode
    {
        private List<ConjunctNode> m_conjuncts;
        /// <summary>
        /// a list of conjuncts which are separated by disjunctions in the exception part of the policy rule.
        /// </summary>
        internal List<ConjunctNode> Conjuncts
        {
            get { return m_conjuncts; }
            set { m_conjuncts = value; }
        }
        /// <summary>
        /// DisjunctNode class constructor
        /// </summary>
        /// <param name="conjuncts">A list of ConjuctNode elements</param>
        public DisjunctNode(List<ConjunctNode> conjuncts)
        {
            this.m_conjuncts = conjuncts;
        }

        public DisjunctNode()
        {
            // TODO: Complete member initialization
        }
        /// <summary>
        /// evaluates the truth value of the DNF clause ie. the disjunction between elements of m_conjuncts
        /// </summary>
        /// <returns>the truth value of the DNF statement</returns>
        public override bool Evaluate(List<PGNode> visitedNodes)
        {
            //if the conjucts list is empty then return false value
            if (this.m_conjuncts.Count == 0)
                return false;

            //get the evaluation of the first conject child
            bool result = this.m_conjuncts[0].Evaluate(visitedNodes);

            //iterate over all the conjuncts in the list and calculate the disjunction
            for (int i = 1; i < this.m_conjuncts.Count(); i++)
                result = result || this.m_conjuncts[i].Evaluate(visitedNodes);
            return result;
        }
    }
}
